<?php
class Core_app
{
	public function __construct()
	{
		$this->_initAutoLoader();
		$this->_initEnv();
		$this->_initInput();
		$this->_initOutput();
	}
	public function run()
	{
		$this->_initAction();
	}
	
	public function _initEnv()
	{
		error_reporting(E_ALL);
		if(PHP_VERSION < '5.3.0') 
		{
			set_magic_quotes_runtime(0);
		}
		define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());
	}
	
	private function _initAutoLoader()
	{
		spl_autoload_register(array($this, 'autoLoader'));
	}
	
	private function _initInput()
	{
		if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) ||  isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS']))
		{
			exit('request_tainting');
		}
		
		if(!MAGIC_QUOTES_GPC) 
		{
			$_GET = Core_functions::dstripslashes($_GET);
			$_POST = Core_functions::dstripslashes($_POST);
			$_COOKIE = Core_functions::dstripslashes($_COOKIE);
		}
	}
	
	private function _initOutput()
	{
		
	}
	
	private function _initAction()
	{
		$m = Core_config::getConfig('WEB_MODULE_NAME');
		$a = Core_config::getConfig('WEB_ACTION_NAME');
		
		$moduleName = isset($_GET[$m]) && !empty($_GET[$m]) ? $_GET[$m] : 'index';
		$moduleClass = APP_NAME.'_controller_'. $moduleName;
		$actionMothod = isset($_GET[$a]) && !empty($_GET[$a]) ? $_GET[$a] : 'index';

		if(!class_exists($moduleClass))
		{
			exit('Module \'' . $moduleName . '\' not found!');
		}
		else if(!method_exists($moduleClass, $actionMothod))
		{
			exit('Action \'' . $moduleClass . '->' . $actionMothod . '\' not found!');
		}
		
		$module = new $moduleClass($moduleName, $actionMothod);
		$module->$actionMothod();
	}
	
	public function autoLoader($className)
	{
		$clsname = $className;
		if (false !== strpos($className, '_'))
		{
			$className = str_replace('_', '/', $className);
			$className = DOC_ROOT . "/{$className}";
		}
		if (!file_exists("{$className}.php"))
		{
			return false;
		}
		require("{$className}.php");
	}
}